//
//  LHAddImageView.m
//  Loohoo
//
//  Created by 陈镇 on 2025/6/9.
//

#import "LHAddImageView.h"
#import "LHAddImageViewCell.h"


@interface LHAddImageView ()<UICollectionViewDelegate,UICollectionViewDataSource,LHAddImageViewCellDelegate>

@property(nonatomic,strong) UICollectionView *collectionView;

@property(nonatomic,strong) NSMutableArray<LHAddImageCellModel *> *selectImages;

@end
@implementation LHAddImageView



-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.selectImages = [NSMutableArray array];


        
        [self addSubview:self.collectionView];
        
        
//        [self createData];
    }
    return self;
}


-(void)createData{
    
    
    LHAddImageCellModel *model = [[LHAddImageCellModel alloc] init];
    model.isSelectAdd = YES;
    model.maxCount = self.config.maxCount;
    model.btnTitle = self.config.btnTitle;
    [self.selectImages addObject:model];
    
    [self.collectionView reloadData];
}

-(void)setConfig:(LHAddImageViewConfig *)config{
    _config = config;
   
    if (self.selectImages.count == 0) {
        [self createData];
    }

    
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
//        _collectionView.backgroundColor = UIColor.greenColor;
        
        [_collectionView registerClass:[LHAddImageViewCell class] forCellWithReuseIdentifier:@"LHAddImageViewCell"];
        
    }
    return _collectionView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, self.width, self.height);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectImages.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LHAddImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LHAddImageViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.model = self.selectImages[indexPath.row];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.config.cell_width) {
        return CGSizeMake(self.config.cell_width, self.config.cell_width);
    }
    CGFloat w = self.width/self.config.lineMax;
    return CGSizeMake(w, w);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return LHAddImageView_gap;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}


#pragma mark - LHAddImageViewCellDelegate

-(void)didDeleteBtn:(LHAddImageCellModel *)model{
    [self.selectImages removeObject:model];
    
    if (!self.selectImages.lastObject.isSelectAdd) { //最有一个不是添加按钮
        if (self.selectImages.count < self.config.maxCount) { //并且不满数量
            [self createData];
        }
    }
   
    self.selectImages.lastObject.count = self.selectImages.count - 1;
    
    [self.collectionView reloadData];
    
    [self refrehLayout];
    

 
    

    
}

-(void)setImages:(NSArray<LHAddImageCellModel *> *)images{
    _images = images;
    [self.selectImages removeAllObjects];
    
    [self.selectImages insertObjects:images atIndex:0];
    
    if (!self.selectImages.lastObject.isSelectAdd) {
        if (self.selectImages.count < self.config.maxCount) {
            [self createData];
            self.selectImages.lastObject.count = self.selectImages.count - 1;
        }
    }
    [self.collectionView reloadData];

}


-(void)didselectAdd{
    
    
    
    
    [LHSelectPhotoManager createManagerCount:self.config.maxCount - self.selectImages.count + 1  block:^(NSArray<UIImage *> * _Nonnull images) {
      
        
       
        
        if (images.count + self.selectImages.count - 1 < self.config.maxCount) {
            
            [self createAllData:images];
         
        }else{
            
        
            [self.selectImages removeLastObject];
            
            [self createAllData:images];
 
     
        }

        
    }];
    
    
    
}

-(CGFloat)getSelfHeight{
    
    CGSize size = self.collectionView.collectionViewLayout.collectionViewContentSize;
    
    self.height = size.height;
    
    return self.height;
}


-(void)refrehLayout{


    if (_delegate && [_delegate respondsToSelector:@selector(refreshLayout:image:)]) {
        
        [_delegate refreshLayout:[self getSelfHeight] image:self.selectImages];
    }
    
    
}
-(void)createAllData:(NSArray *)images{
    
    
    NSMutableArray *tmp = [NSMutableArray array];
    for (int i = 0; i< images.count; i++) {
        LHAddImageCellModel *model = [[LHAddImageCellModel alloc] init];
        model.isSelectAdd = NO;
        model.maxCount = self.config.maxCount;
        model.count = i;
        model.selectImage = images[i];
        [tmp addObject:model];
    }
    
    if (self.selectImages.count > 1) {
        [self.selectImages insertObjects:tmp atIndex:self.selectImages.count - 1];
    }else{
        [self.selectImages insertObjects:tmp atIndex:0];
    }
  
    
    self.selectImages.lastObject.count = self.selectImages.count - 1;
    
    [self.collectionView reloadData];
    
    [self refrehLayout];
    

}
@end




@implementation LHAddImageViewConfig



@end
